package jm.nj.demo22mian.pstmt;

import jm.nj.demo22mian.utils.JdbcUitils;
import org.junit.Test;

import java.sql.*;

public class JdbcPstmtCurdDemo {

    public void demo() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUitils.getConnection();
            String sql = "select * from user where username = ? and password = ?";
            pstmt = conn.prepareStatement(sql);
            //执行参数

            //执行其他操作...

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUitils.closeResource(conn, pstmt, rs);
        }

    }

    @Test
    public void insest() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        //ResultSet rs = null;

        try {
            conn = JdbcUitils.getConnection();
            String sql = "insert into user (username ,password, salary, age, registTime ) values (?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            //执行参数
            pstmt.setString(1, "jerry");
            pstmt.setString(2, "abc");
            pstmt.setDouble(3, 10000);
            pstmt.setInt(4, 40);
            pstmt.setDate(5, Date.valueOf("2017-01-08"));
            //执行其他操作...
            int count = pstmt.executeUpdate();
            System.out.println(count);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUitils.closeResource(conn, pstmt, null);
        }
    }

    @Test

    public void update() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUitils.getConnection();
            String sql = "update user set password = ?  where username= ?";
            pstmt = conn.prepareStatement(sql);
            //执行参数
            pstmt.setString(1, "12345");
            pstmt.setString(2, "jerry");
            //执行其他操作...
            int count = pstmt.executeUpdate();
            ;
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUitils.closeResource(conn, pstmt, rs);
        }

    }

    @Test
    public void query() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUitils.getConnection();
            String sql = "select * from USER ";
            pstmt = conn.prepareStatement(sql);
            //执行参数


            //执行其他操作...
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Integer uid = rs.getInt("uid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Double salary = rs.getDouble("salary");
                Integer age = rs.getInt("age");
                Date registTime = rs.getDate("registTime");
                Timestamp lastupdate = rs.getTimestamp("lastupdate");
                System.out.println(uid + "  " + username + "  " + password + "  " + salary + "  " + age + "  " + registTime + "  " + lastupdate);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUitils.closeResource(conn, pstmt, rs);
        }

    }

    @Test
    public void findById() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUitils.getConnection();
            String sql = "select * from USER  where uid = ?";
            pstmt = conn.prepareStatement(sql);
            //执行参数
            pstmt.setInt(1, 3);
            //执行其他操作...
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Integer uid = rs.getInt("uid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Double salary = rs.getDouble("salary");
                Integer age = rs.getInt("age");
                Date registTime = rs.getDate("registTime");
                Timestamp lastupdate = rs.getTimestamp("lastupdate");
                System.out.println(uid + "  " + username + "  " + password + "  " + salary + "  " + age + "  " + registTime + "  " + lastupdate);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUitils.closeResource(conn, pstmt, rs);
        }

    }

    @Test
    public void delete() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUitils.getConnection();
            String sql = "delete from USER where username = ?";
            pstmt = conn.prepareStatement(sql);
            //执行参数
            pstmt.setString(1, "jerry");
            //执行其他操作...
            int count = pstmt.executeUpdate();
            System.out.println(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUitils.closeResource(conn, pstmt, rs);
        }

    }


}
